/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.cms.rpc.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.lambkit.common.service.LambkitModelServiceImpl;
import com.lambkit.core.aop.AopKit;
import com.lambkit.db.sql.column.Example;
import com.lambkit.module.cms.core.layer.MenuTree;
import com.lambkit.module.cms.rpc.api.CmsPageService;
import com.lambkit.module.cms.rpc.model.CmsPage;
import com.lambkit.module.cms.rpc.model.sql.CmsPageCriteria;
import com.lambkit.module.upms.UpmsManager;
import com.lambkit.module.upms.rpc.model.UpmsUser;

/**
 * @author yangyong
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2019-10-22
 * @version 1.0
 * @since 1.0
 */
public class CmsPageServiceImpl extends LambkitModelServiceImpl<CmsPage> implements CmsPageService {

	private CmsPage DAO = null;

	public CmsPage dao() {
		if (DAO == null) {
			DAO = AopKit.singleton(CmsPage.class);
		}
		return DAO;
	}

	@Override
	public List<CmsPage> linkParentById(Long parentId) {
		// TODO Auto-generated method stub
		List<CmsPage> cs = null;
		CmsPage cmsPage = findById(parentId);
		if (cmsPage != null) {
			cs = new ArrayList<>();
			if (cmsPage.getPid() != null || cmsPage.getPid() != 0) {
				cs = getParentPage(cs, cmsPage.getPid());
			}
			cs.add(cmsPage);
		}
		return cs;
	}

	/**
	 * 遍历父类
	 * 
	 * @param flds
	 * @param id
	 * @return
	 */
	private List<CmsPage> getParentPage(List<CmsPage> flds, Long id) {
		CmsPage catalog = findById(id);
		if (catalog != null) {
			if (catalog.getPid() != null || catalog.getPid() > 0) {
				flds = getParentPage(flds, catalog.getPid());
			}
			flds.add(catalog);
		}
		return flds;
	}

	@Override
	public MenuTree menuTreeByParentId(Long parentId, Long userId) {
		// TODO Auto-generated method stub
		CmsPage cmsPage = findFirst(CmsPage.sql().andPageIdEqualTo(parentId).andUserIdEqualTo(userId).example());
		if(!userId.equals(1L) && !cmsPage.getStatus().equals(CmsPage.STATUS_NORMAL)) {
			return null;
		} 
		if(cmsPage==null) return null;
		return cmsPage.createMenuTree(0);
	}

	@Override
	public CmsPage treeByParentId(Long parentId, Long userId) {
		// TODO Auto-generated method stub
		CmsPage cmsPage = findById(parentId);
		if(cmsPage.getUserId()!=userId) return null;
		if(!userId.equals(1L) && !cmsPage.getStatus().equals(CmsPage.STATUS_NORMAL)) {
			return null;
		} 
		cmsPage.setLayerNumber(0);
		addChild(cmsPage, userId);
		return cmsPage;
	}
	
	private void addChild(CmsPage cmsPage, Long userId) {
		List<CmsPage> list = findByParenId(cmsPage.getPageId(), userId);
		for (int i = 0; i < list.size(); i++) {
			CmsPage m = list.get(i);
			m.setLayerNumber(cmsPage.getLayerNumber() + 1);
			cmsPage.addChild(m);
			addChild(m, userId);
		}
	}

	@Override
	public List<CmsPage> findByParenId(Long parentId, Long userId) {
		// TODO Auto-generated method stub
		UpmsUser user = UpmsManager.me().currentUser();
		CmsPageCriteria criteria = CmsPage.sql().andPidEqualTo(parentId);
		if(user==null || !user.getUserId().equals(1L)) {
			criteria.andStatusEqualTo(CmsPage.STATUS_NORMAL);
		}
		Example example = criteria.example().setOrderBy("orders");
		return find(example);
	}
}
